SPB Git

Repository Public

page.tsx 7.6 KB 128 lines tsx

1import Link from "next/link";2import { nowMs } from "@/server/clock";3import { notFound } from "next/navigation";4import { requireAdmin } from "@/server/session";5import { getDb } from "@/server/db";6import { appointmentById, typeById } from "@/server/booking/service";7import { fmtDateTime } from "@/server/booking/tz";8import { ActionButton, ActionForm } from "@/components/admin/ActionForm";9import { RescheduleForm } from "@/components/admin/RescheduleForm";10import { LOCATION_LABEL, STATUS_BADGE, STATUS_LABEL } from "@/components/admin/status";11import { appointmentAction } from "../../../actions";12import { fmtDateTime as fmtStamp } from "../../../doc";1314export const metadata = { title: "Rendez-vous" };1516export default async function AppointmentPage({ params }: { params: Promise<{ id: string }> }) {17  await requireAdmin();18  const { id } = await params;19  const a = appointmentById(Number(id));20  if (!a) notFound();21  const t = typeById(a.type_id);22  const mails = getDb().prepare("SELECT id, created_at, subject, to_email, status FROM outbox WHERE appointment_id = ? ORDER BY id DESC").all(a.id) as { id: number; created_at: number; subject: string; to_email: string; status: string }[];23  const active = a.status === "requested" || a.status === "confirmed";24  const rows: [string, React.ReactNode][] = [25    ["Rencontre", t?.title ?? "—"],26    ["Date et heure", <span key="d" className="first-letter:uppercase">{fmtDateTime(a.start_utc)}</span>],27    ["Durée", `${Math.round((a.end_utc - a.start_utc) / 60000)} minutes`],28    ["Modalité", t ? [LOCATION_LABEL[t.location_kind], t.location_detail].filter(Boolean).join(" — ") : "—"],29    ["Courriel", a.email ? <a key="e" href={`mailto:${a.email}`} className="text-navy-900 break-all underline underline-offset-2">{a.email}</a> : "—"],30    ["Téléphone", a.phone ? <a key="p" href={`tel:${a.phone.replace(/[^\d+]/g, "")}`} className="text-navy-900 underline underline-offset-2">{a.phone}</a> : "—"],31    ["Organisation", a.organization || "—"],32    ["Motif", <span key="m" className="whitespace-pre-wrap">{a.reason || "—"}</span>],33    ["Notes du client", <span key="n" className="whitespace-pre-wrap">{a.notes || "—"}</span>],34    ["Demandé le", fmtStamp.format(new Date(a.created_at))],35  ];36  if (a.cancel_reason) rows.push(["Motif d’annulation", a.cancel_reason]);37  return (38    <>39      <Link href="/admin/appointments" className="text-ink-700 mb-3 inline-flex min-h-11 items-center text-sm underline underline-offset-4">40        ← Agenda41      </Link>42      <div className="grid gap-6 lg:grid-cols-[minmax(0,1.2fr)_minmax(0,0.8fr)]">43        <article className="card p-5 sm:p-7">44          <div className="flex flex-wrap items-start justify-between gap-3">45            <h1 className="text-navy-900 font-serif text-3xl">{a.name}</h1>46            <span className={`badge ${STATUS_BADGE[a.status]}`}>{STATUS_LABEL[a.status]}</span>47          </div>48          {a.anonymized_at ? <p className="badge badge-grey mt-2">Renseignements personnels supprimés</p> : null}49          <dl className="mt-5 grid gap-x-5 gap-y-3 text-sm sm:grid-cols-[10rem_1fr]">50            {rows.map(([k, v]) => (51              <div key={k} className="contents">52                <dt className="text-ink-500 font-semibold">{k}</dt>53                <dd className="text-ink-900 mb-2 sm:mb-0">{v}</dd>54              </div>55            ))}56          </dl>57          <ActionForm action={appointmentAction} submitLabel="Enregistrer les notes" submitClass="abtn abtn-sm" className="border-pale-300 mt-6 border-t pt-5">58            <input type="hidden" name="id" value={a.id} />59            <input type="hidden" name="op" value="notes" />60            <label htmlFor="admin_notes" className="field-label">61              Notes internes (non visibles par le client)62            </label>63            <textarea id="admin_notes" name="admin_notes" rows={3} defaultValue={a.admin_notes} maxLength={2000} className="field-input" />64          </ActionForm>65        </article>6667        <div className="flex flex-col gap-4">68          {active ? (69            <section className="card p-5" aria-labelledby="act">70              <h2 id="act" className="text-navy-900 font-serif text-xl">71                Actions72              </h2>73              <div className="mt-3 flex flex-wrap gap-2">74                {a.status === "requested" ? <ActionButton action={appointmentAction} hidden={{ id: a.id, op: "confirm" }} label="Accepter et confirmer" className="abtn abtn-primary" /> : null}75                {a.end_utc < nowMs() || a.status === "confirmed" ? <ActionButton action={appointmentAction} hidden={{ id: a.id, op: "complete" }} label="Marquer terminé" className="abtn" /> : null}76              </div>77              <h3 className="text-navy-900 mt-5 text-sm font-semibold">Reporter</h3>78              <div className="mt-2">{t ? <RescheduleForm action={appointmentAction} typeSlug={t.slug} hidden={{ id: a.id, op: "reschedule" }} /> : null}</div>79              <h3 className="text-navy-900 mt-5 text-sm font-semibold">{a.status === "requested" ? "Refuser" : "Annuler"}</h3>80              <ActionForm action={appointmentAction} submitLabel={a.status === "requested" ? "Refuser la demande" : "Annuler le rendez-vous"} submitClass="abtn abtn-danger" confirmText="Confirmer ? Le client sera avisé par courriel et le créneau sera libéré." className="mt-2">81                <input type="hidden" name="id" value={a.id} />82                <input type="hidden" name="op" value="cancel" />83                <label htmlFor="reason" className="field-label">84                  Motif (transmis au client, facultatif)85                </label>86                <input id="reason" name="reason" maxLength={300} className="field-input" />87              </ActionForm>88            </section>89          ) : null}9091          <section className="card p-5" aria-labelledby="mails">92            <h2 id="mails" className="text-navy-900 font-serif text-xl">93              Courriels94            </h2>95            {mails.length === 0 ? (96              <p className="text-ink-500 mt-2 text-sm">Aucun courriel lié à ce rendez-vous.</p>97            ) : (98              <ul className="mt-2 flex flex-col gap-2 text-sm">99                {mails.map((m) => (100                  <li key={m.id} className="flex items-start justify-between gap-2">101                    <span className="min-w-0">102                      <span className="text-ink-900 block truncate">{m.subject}</span>103                      <span className="text-ink-500 block truncate text-xs">104                        à {m.to_email} · {fmtStamp.format(new Date(m.created_at))}105                      </span>106                    </span>107                    <span className={`badge ${m.status === "sent" ? "badge-green" : m.status === "failed" ? "badge-red" : "badge-grey"}`}>{m.status === "sent" ? "Envoyé" : m.status === "failed" ? "Échec" : "Non envoyé"}</span>108                  </li>109                ))}110              </ul>111            )}112          </section>113114          {!a.anonymized_at ? (115            <section className="card p-5">116              <h2 className="text-navy-900 font-serif text-xl">Données personnelles</h2>117              <p className="text-ink-700 mt-1 text-sm">Efface le nom, le courriel, le téléphone, le motif et les notes de ce rendez-vous{active ? " (et l’annule)" : ""}.</p>118              <div className="mt-3">119                <ActionButton action={appointmentAction} hidden={{ id: a.id, op: "anonymize" }} label="Supprimer les renseignements" className="abtn abtn-sm abtn-danger" confirmText="Supprimer définitivement les renseignements personnels de ce rendez-vous ?" />120              </div>121            </section>122          ) : null}123        </div>124      </div>125    </>126  );127}128
Files main
Menu

Theme